#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include "staff.h"

int main(int argc, const char *argv[])
{
	Msg msg;
	char *errmsg;

	//创建一个数据库  
	if(sqlite3_open(STAFF_SQL,&db) != SQLITE_OK)
	{
		printf("%s.\n",sqlite3_errmsg(db));
	}else{
		printf("staff sqlite create success\n");
	}

	//创建两个表 员工表  和 历史记录表
	if(sqlite3_exec(db,"create table usrinfo(id integer primary key,usertype integer,name text,passwd text,age integer,telphone text,\
		address text,position text,data_entry text,rate integer,salary REAL);",NULL,NULL,&errmsg)!= SQLITE_OK)
	{
		printf("%s.\n",errmsg);
	}else
	{
		printf("create usrinfo table success.\n");
	}

	if(sqlite3_exec(db,"create table historyinfo(time text,words text);",NULL,NULL,&errmsg)!= SQLITE_OK){
		printf("%s.\n",errmsg);
	}else{ 
		printf("create historyinfo table success.\n");
	}

	if(sqlite3_exec(db,"insert into usrinfo values (0,1,'admin','123456',18,'18364006028','heze','inte','2021.3.1','A','10000');",NULL,NULL,&errmsg)!= SQLITE_OK){
		printf("%s.\n",errmsg);
	}else{ 
		printf("create admin:123456 success.\n");
	}


	// 创建一个套接字socket
	int fd;
	fd = socket(AF_INET,SOCK_STREAM,0);
	if(fd < 0){
		perror("socket");
		exit(1);
	}
	puts("--创建套接字成功--");

	int optval = 1;
	socklen_t optval_len = sizeof(optval);
	setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&optval,optval_len);

	// 填充自己的服务器IP地址信息结构体
	struct sockaddr_in sin;
	bzero(&sin,sizeof(sin));
	sin.sin_family = AF_INET;
	sin.sin_port = htons(atoi(argv[2]));
	sin.sin_addr.s_addr = inet_addr(argv[1]);//INADDR_ANY  所有的网卡都能提供服务
	puts("--填充服务器结构体成功--");

	// bind 绑定fd
	int ret_bind = bind(fd,(struct sockaddr*)&sin,sizeof(sin));
	if(ret_bind < 0){
		perror("bind");
		close(fd);
		exit(2);
	}
	puts("--地址信息绑定成功，服务器开始工作--");


	// listen 监听
	int ret_lis = listen(fd,128); //BACKLOG 128
	if(ret_lis < 0){
		perror("listen");
		exit(1);
	}

	//定义一个文件描述符的集合
	fd_set staff_fds,temp_fds;
	FD_ZERO(&staff_fds);
	FD_ZERO(&temp_fds);
	FD_SET(fd,&staff_fds);

	//定义一个存储的客户端的地址信息结构体
	struct sockaddr_in cin;
	bzero(&cin,sizeof(cin));
	socklen_t addrlen = sizeof(cin);


	int val = -1;
	int newfd = -1;
	int i;
	ssize_t ret_rcv = -1;
	int maxfd = fd;
	while(1){
		temp_fds = staff_fds;
		val = select(maxfd+1,&temp_fds,NULL,NULL,0);
		if(val < 0){
			perror("select");
			continue;
		}
		else if(val > 0){
			for(i = 3;i<maxfd+1;i++){
				if(FD_ISSET(i,&temp_fds)){
					if(i==fd){
						newfd = accept(fd,(struct sockaddr*)&cin,&addrlen);
						if(newfd < 0){
							perror("accept");
							exit(1);
						}
						printf("--%s-%d-已连接--\n",(char *)inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
						FD_SET(newfd,&staff_fds);
						maxfd = maxfd > newfd?maxfd:newfd;
					}
					else if(i>fd){
						ret_rcv = recv(i,&msg,sizeof(Msg),0);
						if(ret_rcv < 0){
							perror("recv");
							break;
						}

						else if(ret_rcv == 0){
							FD_CLR(i,&staff_fds);
							puts("对方关闭\n");
							break;
						}else{
							process_client_request(i,&msg);
						}
					}
				}
			}
		}
	}
	close(fd);
	close(newfd);
	return 0;
}

